##  pip3 install baidu-aip
import base64
from aip import AipFace


#
# """ 你的 APPID AK SK """
# APP_ID = '119414949'
# API_KEY = 'gMVpzD4hiRMwSeojYrRlRxs6'
# SECRET_KEY = 'dSM392eHbW9Lck8enb7yIU8XV62nfyHc'
#
# client = AipFace(APP_ID, API_KEY, SECRET_KEY)

class BaiduFace:
    def __init__(self, APP_ID='119414949', APP_KEY='gMVpzD4hiRMwSeojYrRlRxs6',
                 APP_SECRET='dSM392eHbW9Lck8enb7yIU8XV62nfyHc'):
        self.APP_ID = APP_ID
        self.APP_KEY = APP_KEY
        self.APP_SECRET = APP_SECRET

        self.client = AipFace(self.APP_ID, self.APP_KEY, self.APP_SECRET)

    # 注册人脸  611223cdffb97a8ce416e0ef9dbd5bc1
    def add_user(self):
        image = base64.b64encode(open('./fengjuan.jpg', 'rb').read()).decode(
            'utf-8')  # 611223cdffb97a8ce416e0ef9dbd5bc1
        imagType = "BASE64"
        groupId = "mingle"
        userId = "fengjuan"

        """ 调用人脸注册 """
        # client.addUser(image, imagType, groupId, userId);

        options = {}
        options["user_info"] = "wife"
        options["quality_control"] = "NORMAL"
        options["liveness_control"] = "LOW"
        options["action_type"] = "REPLACE"

        """ 带参数调用人脸注册 """
        res = self.client.addUser(image, imagType, groupId, userId, options)
        return res

    # 删除人脸
    def delete(self):
        userId = "mingle1"
        groupId = "mingle"
        faceToken = "611223cdffb97a8ce416e0ef9dbd5bc1"  # 传入录入时生成的 face_token =  d30aae77fcf418fdaee4f8f3a93b3d3c

        """ 调用人脸删除 """
        res = self.client.faceDelete(userId, groupId, faceToken)
        return res

    # 搜索人脸
    def search(self):
        data = base64.b64encode(open('./me.png', 'rb').read()).decode('utf-8')
        image = data
        imageType = "BASE64"
        groupIdList = "mingle,test"

        """ 调用人脸搜索 """
        self.client.search(image, imageType, groupIdList)

        """ 如果有可选参数 """
        options = {}
        options["match_threshold"] = 70
        options["quality_control"] = "NORMAL"
        options["liveness_control"] = "LOW"
        # options["user_id"] = "233451"

        options["max_user_num"] = 3
        # 带参数调用人脸搜索
        res = self.client.search(image, imageType, groupIdList, options)
        return res


if __name__ == '__main__':
    ai = BaiduFace()
    ## 注册新增
    # res = ai.add_user()  # 如果人脸库已存在则替换，否则新增 611223cdffb97a8ce416e0ef9dbd5bc1
    # {'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 3624891282, 'timestamp': 1751637055, 'cached': 0, 'result': {'face_token': '611223cdffb97a8ce416e0ef9dbd5bc1', 'location': {'left': 123.55, 'top': 147.32, 'width': 258, 'height': 271, 'rotation': 1}}}
    # {'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 2256013737, 'timestamp': 1751637519, 'cached': 0, 'result': {'face_token': 'd30aae77fcf418fdaee4f8f3a93b3d3c', 'location': {'left': 195.55, 'top': 223.23, 'width': 249, 'height': 249, 'rotation': -2}}}

    ## 删除注销
    # res = ai.delete()  # {'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 170357660, 'timestamp': 1751638046, 'cached': 0, 'result': True}

    # 搜索人脸
    res = ai.search()
    # {'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 1622917167, 'timestamp': 1751638350, 'cached': 0, 'result': {'face_token': '611223cdffb97a8ce416e0ef9dbd5bc1', 'user_list': [{'group_id': 'mingle', 'user_id': 'mingle1', 'user_info': '我自己', 'score': 99.9971923828125}]}}
    print(res)  # score 匹配度，原图匹配度达到了 99.99%  同一个人不同图片 96.14%
